{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 自定义函数中混合使用*args和**kwargs\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import statistics\n",
    "\n",
    "\n",
    "# 混合 *args, **kwargs\n",
    "def calc_stats(operation, *args, **kwargs):\n",
    "    result = 0\n",
    "    # 计算标准差\n",
    "    if operation == \"stdev\":\n",
    "        # 总体标准差\n",
    "        if \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'population':\n",
    "            result = statistics.pstdev(args)\n",
    "        # 样本标准差\n",
    "        elif \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'sample':\n",
    "            result = statistics.stdev(args)\n",
    "        else:\n",
    "            raise ValueError('TYPE, either population or sample')\n",
    "    # 计算方差\n",
    "    elif operation == \"var\":\n",
    "        # 总体方差\n",
    "        if \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'population':\n",
    "            result = statistics.pvariance(args)\n",
    "        # 样本方差\n",
    "        elif \"TYPE\" in kwargs and kwargs[\"TYPE\"] == 'sample':\n",
    "            result = statistics.variance(args)\n",
    "        else:\n",
    "            raise ValueError('TYPE, either population or sample')\n",
    "    else:\n",
    "        print(\"Unsupported operation\")\n",
    "        return None\n",
    "    # 保留小数位\n",
    "    if \"ROUND\" in kwargs:\n",
    "        result = round(result, kwargs[\"ROUND\"])\n",
    "    return result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:33:05.216551Z",
     "end_time": "2024-07-03T16:33:05.231431Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "1.708"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算总体标准差\n",
    "calc_stats(\"stdev\", 1, 2, 3, 4, 5, 6,\n",
    "           TYPE='population', ROUND=3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:33:13.672524Z",
     "end_time": "2024-07-03T16:33:13.698649Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "1.8708286933869707"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算样本标准差\n",
    "calc_stats(\"stdev\", 1, 2, 3, 4, 5, 6, TYPE='sample')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:33:30.906956Z",
     "end_time": "2024-07-03T16:33:30.918101Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "2.9167"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算总体方差\n",
    "calc_stats(\"var\", 1, 2, 3, 4, 5, 6,\n",
    "           TYPE='population', ROUND=4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:33:45.745570Z",
     "end_time": "2024-07-03T16:33:45.756957Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "3.5"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算样本方差\n",
    "calc_stats(\"var\", 1, 2, 3, 4, 5, 6, TYPE='sample')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:33:54.118995Z",
     "end_time": "2024-07-03T16:33:54.129491Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
